Page({

  data: {
    menuId:"",
    searchInputStyle: "",
    searchStyle: "",
    menuScroll: false,
    // 菜单列表
    menuList: [],
    selectedMenuId: 0,
    totalPrice: 0,
    proNum: 0 
  },

  onLoad() {
    this.getMenuPosition()
    this.getMenuList()
  },

  // 支付
  goPay(){
    //验证用户身份(这里一般使用token来验证)
    // 本次使用简单的
    let userInfo =  wx.getStorageSync('userInfo')
    console.log(userInfo)
    if(userInfo == null || userInfo == ''){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }else{
      //选中的商品详细数据
      let proAry = new Array();
      for (const menu of this.data.menuList) {
        for (const pro of menu.productList) {
          if(pro.num > 0){
            // 将选中的商品放置到proAry中,用于订单详情的时候使用
            proAry.push(pro)
          }  
        } 
      }
      //创建一个订单对象
      let order = {
        projectNum:this.data.proNum,
        totalPrice:this.data.totalPrice,
        userId:userInfo.id,
        proAry:proAry
      }
      // 发送请求 
      wx.request({
        url: 'http://localhost:9527/orders/',
        method:"POST",
        data:order,
        success: res => {
          if(res.data.code == 0){
            this.getMenuList()
            wx.switchTab({
              url: '/pages/order/order',
            }) 
          }
        }
      })
      



    }
  },
  // 选择商品
  chooseProduct(event) { 
    let {
      menuid,
      proid,
      choose
    } = event.currentTarget.dataset
    // 获取页面所有商品信息列表(用来临时缓存数据)
    let meList = this.data.menuList

    // 计算选中商品的总量
    let pro_num = 0
    // 计算选中商品的总价
    let total_price = 0
   
    // 给每个选中的商品进行 num数量字段的添加和减少
    for (const menu of meList) {
      if(menu.id == menuid){
        for (const pro of menu.productList) {
          if(pro.id == proid){
            if(choose == 'add' & pro.num < 10){
              pro.num = pro.num + 1
            }
            if(choose == 'del' & pro.num > 0){
              pro.num = pro.num - 1
            }
          }  
        }
      }
    }
    // 计算总量以及总价格
    for (const menu of meList) {
      for (const pro of menu.productList) {
        pro_num =  pro_num + pro.num
        if(pro.discountPrice == null){
          total_price = total_price + (pro.num * pro.price)
        }else{
          
          total_price = total_price + (pro.num * pro.discountPrice)
         
        }
      } 
    }

    this.setData({
      menuList:meList,
      proNum:pro_num,
      totalPrice:total_price
    })
  },
  // 更改菜单选项
  chooseMenu(event) {
    let id = event.currentTarget.dataset.id
    this.setData({
      menuId:'menu-'+id,
      selectedMenuId: id
    })
  },

  //获取胶囊的位置信息
  getMenuPosition() {
    let {
      height,
      top
    } = wx.getMenuButtonBoundingClientRect()
    //  输入框的高度
    let serchInputHeight = "height:" + height + "px"
    // 输入框的位置
    let searchTop = "margin-top:" + top + "px"

    this.setData({
      searchInputStyle: serchInputHeight,
      searchStyle: searchTop
    })
  },
  //获取菜单栏列表数据
  getMenuList() {
    wx.request({
      url: 'http://localhost:9527/menus/',
      method: "GET",
      success: res => {
        this.setData({
          menuList: res.data.data,
          selectedMenuId: res.data.data[0].id
        })
      }
    })
  }

})